#include "lv_port_tick.h"

#include "at32f403a_407.h"

void TMR1_OVF_TMR10_IRQHandler(void)
{
  if(tmr_flag_get(TMR1, TMR_OVF_FLAG) != RESET)
  {
    lv_tick_inc(LV_TICK_PERIOD_MS);
    tmr_flag_clear(TMR1, TMR_OVF_FLAG);
  }
}

void lv_create_tick()
{
    crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
    
    /* get system clock */
    crm_clocks_freq_type crm_clocks_freq_struct = {0};
    crm_clocks_freq_get(&crm_clocks_freq_struct);

    tmr_base_init(TMR1, LV_TICK_PERIOD_MS*10 - 1, (crm_clocks_freq_struct.ahb_freq / 10000) - 1);
    tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);

    /* overflow interrupt enable */
    tmr_interrupt_enable(TMR1, TMR_OVF_INT, TRUE);

    /* tmr1 overflow interrupt nvic init */
    nvic_irq_enable(TMR1_OVF_TMR10_IRQn, 0, 0);
    
    /* enable tmr1 */
    tmr_counter_enable(TMR1, TRUE);
}